import { _decorator, Component, Node, Vec2, ProgressBar, Label, instantiate, Contact2DType, PolygonCollider2D, Collider2D } from 'cc';
import { Battle } from '../../Battle';
import { EnDynamicLineType } from '../../Common/const/EnCommon';
import { EnEvent } from '../../Common/const/EnEvent';
import { GameAPI } from '../../Common/GameAPI';
import { PlayerEnergyManager } from '../../Player/PlayerEnergyManager';
import { NumberSplitter } from '../NumberSplitter';
const { ccclass, property } = _decorator;

@ccclass('HeroTowerController')
export class HeroTowerController extends Component {

    @property(NumberSplitter)
    private numSplitter: NumberSplitter = null;
    @property(ProgressBar)
    private levelProgress: ProgressBar = null;
    @property(Number)
    private needEnergyType: number = 1;

    @property(Node)
    private energyUseNode: Node = null;

    private curLevel: number = 0;
    private curRequiredNum: number = 0;
    private maxLevel: number = 6;

    private levelEnergyValues: Array<number> = [];

    private collider: PolygonCollider2D;
    private isFeeding: boolean = false;

    private multiplyingPower: number = 1;

    private checkTime: number = 0;

    private energyManager: PlayerEnergyManager;

    start() {
        this.levelEnergyValues.push(20);
        this.levelEnergyValues.push(50);
        this.levelEnergyValues.push(120);
        this.levelEnergyValues.push(260);
        this.levelEnergyValues.push(99999);

        this.curLevel = 0;
        this.UpgradeTower();
        this.maxLevel = this.levelEnergyValues.length;
        // 碰撞监听
        this.collider = this.node.getComponent(PolygonCollider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            this.collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
        this.checkTime = 1;
        this.energyManager = Battle.playerNode.getComponent(PlayerEnergyManager);
    }

    update(deltaTime: number) {
        this.checkTime -= deltaTime;
        if (this.checkTime <= 0) {
            this.checkTime = 1;
            this.checkGou();
        }
    }

    checkGou() {
        if (this.energyManager) {
            let energyNum = this.energyManager.GetEnergyNumByType(this.needEnergyType);
            if (energyNum - this.curRequiredNum >= 0) {
                //够升级
                GameAPI.FireGEvent(EnEvent.DynamicLineEvent, { type: EnDynamicLineType.CallHero });
            }
        }
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    // 碰撞开始回调
    onBeginContact(self: Collider2D, other: Collider2D) {
        // 碰到主角
        if (other.node.name.includes("Player")) {
            if (!this.isFeeding) {
                if (this.curRequiredNum >= 35 && this.curRequiredNum <= 60) {
                    this.multiplyingPower = 2;
                }
                else if (this.curRequiredNum > 60 && this.curRequiredNum <= 120) {
                    this.multiplyingPower = 4;
                }
                else if (this.curRequiredNum > 120) {
                    this.multiplyingPower = 5;
                }

                let myThis = this;
                this.schedule(function () {
                    myThis.UpdateLevelProgress();
                }.bind(this), 0.05);
                this.isFeeding = true;
            }
        }
    }

    // 碰撞结束回调
    onEndContact() {
        this.isFeeding = false;
        this.unscheduleAllCallbacks();
    }

    // update(deltaTime: number) {
    //     if (this.isUse) {
    //         this.timer += deltaTime;
    //         if (this.timer >= 0.1) {
    //             this.CreatorItem(this.energyUseNode);
    //             this.timer = 0;
    //         }
    //     }
    // }

    OnEnergyUseEvent(arg) {
        // if (arg.use == false) {
        //     this.isUse = false;
        // }
        // else {
        //     if (arg.index == 1) {
        //         this.isUse = true;
        //     }
        // }
    }

    CreatorItem(prefab) {
        let obj = instantiate(prefab);
        this.node.addChild(obj);
        let pos = this.energyUseNode.position;
        obj.setPosition(pos);
        obj.active = true;
    }

    UpdateLevelProgress() {
        if (this.curLevel >= this.maxLevel) {
            return;
        }
        let energyNum = Battle.playerNode.getComponent(PlayerEnergyManager).GetEnergyNumByType(this.needEnergyType);
        if (energyNum > 0) {
            let power = this.multiplyingPower * 1;
            if (power > energyNum) {
                power = energyNum;
            }
            this.curRequiredNum -= power;
            this.energyManager.RemoveEnergyNumByType(this.needEnergyType, power);
            if (this.curRequiredNum <= 0) {
                GameAPI.FireGEvent(EnEvent.AddHeroEvent);
                this.curLevel++;
                this.UpgradeTower();
                this.unscheduleAllCallbacks();
                GameAPI.FireGEvent(EnEvent.DynamicLineEvent, { type: EnDynamicLineType.Default });
            }
            else {
                this.CreatorItem(this.energyUseNode);
                this.UpdateProgressBar();
            }
        }

        // const playerWorldPos = Battle.playerNode.getWorldPosition();
        // const distance = Vec2.distance(this.node.getWorldPosition(), playerWorldPos);
        // if (distance <= 100) {
        //     let energyNum = Battle.playerNode.getComponent(PlayerEnergyManager).GetEnergyNum(this.needEnergyType);
        //     if (energyNum > 0) {
        //         this.curRequiredNum -= 1;
        //         if (this.curRequiredNum <= 0) {
        //             GameAPI.FireGEvent(EnEvent.AddHeroEvent);
        //             this.curLevel++;
        //             this.UpgradeTower();
        //         }
        //         else {
        //             this.CreatorItem(this.energyUseNode);
        //             this.UpdateProgressBar();
        //         }
        //     }
        // }
    }

    UpgradeTower() {
        if (this.curLevel + 1 == this.maxLevel) {
            this.node.active = false;
            return;
        }
        this.curRequiredNum = this.levelEnergyValues[this.curLevel];
        // this.levelLabelNum.string = "x" + this.curRequiredNum.toString();
        this.numSplitter.setNumber(this.curRequiredNum);
        this.levelProgress.progress = 0;
    }

    UpdateProgressBar() {
        let total = this.levelEnergyValues[this.curLevel];
        let progress = this.curRequiredNum; //total - this.curRequiredNum;
        // this.levelLabelNum.string = progress + "/" + total;
        // this.levelLabelNum.string = "x" + progress.toString();
        this.numSplitter.setNumber(this.curRequiredNum);
        this.levelProgress.progress = progress / total;
    }

}


